using UnityEngine;
using System.Collections;

public class GamePlane : MonoBehaviour {
	public Vector2 p1, p2, p3, p4;
	Vector2 rp1, rp2, rp3, rp4;
	public int scaleFactor;
	
	float left, top, right, bottom;
	string p1_x, p1_y, p2_x, p2_y, p3_x, p3_y, p4_x, p4_y;
	
	// Use this for initialization
	void Start () {
		rp1 = new Vector2(p1.x, p1.y);
		rp2 = new Vector2(p2.x, p2.y);
		rp3 = new Vector2(p3.x, p3.y);
		rp4 = new Vector2(p4.x, p4.y);
		
		p1_x = ""+rp1.x;
		p1_y = ""+rp1.y;
		p2_x = ""+rp2.x;
		p2_y = ""+rp2.y;
		p3_x = ""+rp3.x;
		p3_y = ""+rp3.y;
		p4_x = ""+rp4.x;
		p4_y = ""+rp4.y;
		
		UpdateGamePlaneSRT();
		
		//GameObject.FindGameObjectWithTag("GamePlane").GetComponent<GamePlane>().UpdateGamePlaneSRT
	}
	
	// Update is called once per frame
	void Update () {
		//p1 = new Vector2(0.5f, 0.0f);
	}
	
	public void UpdateGamePlaneSRT() {
		left = rp1.x;
		if( left > rp2.x )
			left = rp2.x;
		if( left > rp3.x )
			left = rp3.x;
		if( left > rp4.x ) 
			left = rp4.x;
		
		right = p1.x;
		if( right < rp2.x )
			right = rp2.x;
		if( right < rp3.x )
			right = rp3.x;
		if( right < rp4.x ) 
			right = rp4.x;
		
		top = rp1.y;
		if( top > rp2.y )
			top = rp2.y;
		if( top > rp3.y )
			top = rp3.y;
		if( top > rp4.y ) 
			top = rp4.y;
		
		bottom = rp1.y;
		if( bottom < rp2.y )
			bottom = rp2.y;
		if( bottom < rp3.y )
			bottom = rp3.y;
		if( bottom < rp4.y ) 
			bottom = rp4.y;
		
		// Debug.Log(left + " " + right + " " + top + " " + bottom);
		renderer.transform.localPosition = new Vector3(scaleFactor*(left+right-1)/2, 0.1f, scaleFactor*(top+bottom-1)/2);
		renderer.transform.localScale = new Vector3(scaleFactor*(right-left), scaleFactor*(bottom-top), 0.1f);
	}
	
	void OnGUI () {
		p1_x = GUI.TextField(new Rect (0.0f*Screen.width, 0.1f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p1_x, 25);
		p1_y = GUI.TextField(new Rect (0.05f*Screen.width, 0.1f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p1_y, 25);
		p2_x = GUI.TextField(new Rect (0.0f*Screen.width, 0.15f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p2_x, 25);
		p2_y = GUI.TextField(new Rect (0.05f*Screen.width, 0.15f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p2_y, 25);
		p3_x = GUI.TextField(new Rect (0.0f*Screen.width, 0.2f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p3_x, 25);
		p3_y = GUI.TextField(new Rect (0.05f*Screen.width, 0.2f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p3_y, 25);
		p4_x = GUI.TextField(new Rect (0.0f*Screen.width, 0.25f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p4_x, 25);
		p4_y = GUI.TextField(new Rect (0.05f*Screen.width, 0.25f*Screen.height, 0.05f*Screen.width, 0.05f*Screen.height), p4_y, 25);
		
		if( GUI.Button(new Rect(0.0f*Screen.width, 0.3f*Screen.height, 0.1f*Screen.width, 0.05f*Screen.height), "REFRESH" ) ){
			rp1.x = float.Parse(p1_x);
			rp1.y = float.Parse(p1_y);
			rp2.x = float.Parse(p2_x);
			rp2.y = float.Parse(p2_y);
			rp3.x = float.Parse(p3_x);
			rp3.y = float.Parse(p3_y);
			rp4.x = float.Parse(p4_x);
			rp4.y = float.Parse(p4_y);
			UpdateGamePlaneSRT();
		}
		
		GUI.Button(new Rect(0.0f, 0.0f, Screen.width, 0.1f*Screen.height), "left : " + left + " right : " + right + " top : " + top + " bottom : " + bottom + "\nx : " + transform.localPosition.x + " y : " + transform.localPosition.z + " scale x : " + transform.localScale.x + " scale y : " + transform.localScale.y);
	}
}
